$(function(){

    var param = $("#tabContent > .tab-pane.active").data();
    paramTabPage[tabId]=param;
    paramTabPage[tabId].isExceeding="1";
    $("#searchDetailId").click(function(){

        paramTabPage[tabId].isPrimaryFee=$("input[name='primaryFee']:checked").val();
        paramTabPage[tabId].isPrimaryUse=$("input[name='primaryUse']:checked").val();
        paramTabPage[tabId].isHighOrder=$("input[name='highValue']:checked").val();
        if(paramTabPage[tabId].isPrimaryFee == "custom"){
            if($("#primaryFee").val()){
                paramTabPage[tabId].isPrimaryFee = $("#primaryFee").val();
            }else{
                drgUI.warning('请输入自定义值!');
                return;
            }
        }
        if(paramTabPage[tabId].isPrimaryUse == "custom"){
            if($("#primaryUse").val()){
                paramTabPage[tabId].isPrimaryUse = $("#primaryUse").val();
            }else{
                drgUI.warning('请输入自定义值!');
                return;
            }
        }
        if(paramTabPage[tabId].isHighOrder == "custom"){
            if($("#highValue").val()){
                paramTabPage[tabId].isHighOrder = $("#highValue").val();
            }else{
                drgUI.warning('请输入自定义值!');
                return;
            }
        }
        var notExcedding = $("input[name='notExceddingCount']:checked").val()
        if(notExcedding == "custom"){
            if($("#notExceddingCount").val()){
                paramTabPage[tabId].NotExceedingOrders = $("#notExceddingCount").val();
            }else{
                drgUI.warning('请输入未超标使用人数!');
                return;
            }
        }else{
            paramTabPage[tabId].NotExceedingOrders=$("input[name='notExceddingCount']:checked").val();
        }

        //查询总体值
        $.post(contextPath + "/order/getOrderAnalysisResultTotal.dhc",paramTabPage[tabId]).done(function(data){
            var data = data.data;
            $("#"+tabId+" #orderCount").html(formatNumber(data.ORDERCOUNT));
            $("#"+tabId+" #orderPt").html(formatNumber(data.ORDERPT));
            $("#"+tabId+" #mrCount").html(formatNumber(data.MRCOUNT));
            $("#"+tabId+" #orderAvgPt").html(formatNumber(data.ORDERAVGPT));
            $("#"+tabId+" #orderTotalFee").html(formatNumber((data.ORDERTOTALFEE/10000).toFixed(2)));
            $("#"+tabId+" #orderAvgFee").html(formatNumber(data.ORDERAVGFEE));
            $("#"+tabId+" #totalFee").html(formatNumber((data.TOTALFEE/10000).toFixed(2)));
            $("#"+tabId+" #orderFeeRatio").html(data.ORDERFEERATIO);
        });

        $.post(contextPath + "/order/getResult.dhc",paramTabPage[tabId]).done(function(data){
            var columns=[
                {
                    "data": ' ',
                    "sClass": 'details-control',
                    "sWidth":20
                },
                { "data": 'ORDERID'},
                { "data": 'ORDERDESC'},
                { "data": 'MaxUnitPrice1',
                    "render": function(obj, type, full){
                        var maxUnitPrice = full.MAXUNITPRICE;
                        if(maxUnitPrice && parseFloat(maxUnitPrice)>500){
                            return "<label style='color:red'>"+formatNumber(maxUnitPrice)+"</label>"
                        }
                        return formatNumber(maxUnitPrice);
                    }
                },
                { "data": 'MrCount',
                    "render": function(obj, type, full){
                        return formatNumber(full.MRCOUNT);
                    }
                },
                { "data": 'OrderTotalFee',
                    "render": function(obj, type, full){
                        return formatNumber(full.ORDERTOTALFEE);
                    }
                },
                { "data": 'OrderAvgFee',
                    "render": function(obj, type, full){
                        return formatNumber(full.ORDERAVGFEE);
                    }
                },
                { "data": 'OrderQuality',
                    "render": function(obj, type, full){
                        return formatNumber(full.ORDERQUALITY);
                    }
                },
                { "data": 'OrderCount',
                    "render": function(obj, type, full){
                        return formatNumber(full.ORDERCOUNT);
                    }
                },
                { "data": 'UsedRate1',
                    "render": function(obj, type, full){
                        var usedRate = full.USAGERATE;
                        if(usedRate && parseFloat(usedRate)>60){
                            return "<label style='color:red'>"+usedRate+"</label>"
                        }
                        return usedRate;
                    }
                },
                { "data": 'NotExcellent',
                    "render": function(obj, type, full){
                        return formatNumber(full.NOTEXCELLENT);
                    }
                }
            ];
            var paramsDataTables={pageLength:20,lengthChange:false,stateSave:true,tableTitle:'医嘱费用明细表',ordering:true,order:[[4,'desc']],columnDefs:[{"orderable": false,"targets": [ 0] }]};
            var oaTable = $("#"+tabId+" #orderResult-table").dataTableBasic(columns,data.data,paramsDataTables);
            $('#orderResult-table tbody').off("click", "td.details-control");
            $('#orderResult-table tbody').on('click', 'td.details-control', function () {
                var nTr = $(this).closest('tr');
                var row = oaTable.row( nTr );
                if (row.child.isShown()) //判断是否已打开
                {
                    row.child.hide();
                    nTr.removeClass("shown");
                } else {
                    nTr.addClass("shown");
                    // 调用方法显示详细信息 data_id为自定义属性 存放配置ID
                    fnFormatDetails(row, $(this).parents('tr').find("td").eq(1).html());
                }
            } );
            function fnFormatDetails(row, pdataId){
                row.child("正在加载...").show();
                try{
                    var param = jQuery.extend(true, {}, paramTabPage[tabId]);
                    param.orderId = pdataId;
                    $.ajax({
                        url: contextPath + "/order/getResultDetail.dhc",
                        data: param,
                        async:true,
                        type:"post",
                        success: function(data){
                            var str = '';
                            str+=' <table "  ';
                            str+=' width="70%" ';
                            str+=' 	>  ';
                            str+=' 	<thead>  ';
                            str+=' 		<tr>  ';
                            str+=' 			<td></td>  ';
                            str+=' 			<td>使用人次</td>  ';
                            str+=' 			<td>医嘱总金额(元)</td>  ';
                            str+=' 			<td>例均金额(元)<a title="按人头计算"><sup style="cursor: hand;">[3]</sup></a></td>  ';
                            str+=' 			<td>医嘱数量</td>  ';
                            str+=' 			<td>医嘱次数</td>  ';
                            str+=' 			<td>使用率(%)</td>  ';
                            str+=' 		</tr>  ';
                            str+=' 	</thead>  ';
                            str+=' 	<tbody>  ';
                            for(var o in data.data){
                                str += "<tr>";
                                str += "<td>"+(data.data[o].ddesc==undefined?"-":data.data[o].ddesc)+"</td>";
                                str += "<td>"+(data.data[o].MRCOUNT==undefined?"-":data.data[o].MRCOUNT)+"</td>";
                                str += "<td>"+(data.data[o].ORDERTOTALFEE==undefined?"-":data.data[o].ORDERTOTALFEE)+"</td>";
                                str += "<td>"+(data.data[o].ORDERAVGFEE==undefined?"-":data.data[o].ORDERAVGFEE)+"</td>";
                                str += "<td>"+(data.data[o].ORDERQUALITY==undefined?"-":data.data[o].ORDERQUALITY)+"</td>";
                                str += "<td>"+(data.data[o].ORDERCOUNT==undefined?"-":data.data[o].ORDERCOUNT)+"</td>";
                                str += "<td>"+(data.data[o].USAGERATE==undefined?"-":data.data[o].USAGERATE)+"</td>";
                                str += "</tr>";
                            }
                            str+=' 	</tbody>  ';
                            str+=' </table>  ';
                            row.child(str).show();
                        }
                    })
                }catch(e){
                    row.child("出错了...").show();
                }
            }
        });
    })
    $("#searchDetailId").click();

})